package com.web.portal.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.web.portal.entity.TutoringCenter;
import com.web.portal.exception.PortalException;
import com.web.portal.service.TutoringCenterService;

@Controller
public class TutoringCenterController {
	@Autowired
	TutoringCenterService tutoringCenterService;

	public TutoringCenterService getTutoringCenterService() {
		return tutoringCenterService;
	}

	public void setTutoringCenterService(TutoringCenterService tutoringCenterService) {
		this.tutoringCenterService = tutoringCenterService;
	}
	@RequestMapping (value="/admin/gettutoringcenter.do")
	public String getListOfTutoringCenter(Map<String,Object>model) throws PortalException{
		TutoringCenter tutoringCenter= new TutoringCenter();
		List<TutoringCenter> tutoringCenterLists =tutoringCenterService.getList(tutoringCenter);
		TutoringCenter tutoringCent=null;
		if(!tutoringCenterLists.isEmpty()){
			tutoringCent=tutoringCenterLists.get(0);
		}
		List<TutoringCenter>  tutoringCenterList= new ArrayList<TutoringCenter>();
		tutoringCenterList.add(tutoringCent);
		model.put("tutoringCenterList", tutoringCenterList);
		return "tutoring_center_view";
	}
	/*
	 * Add tutoring center from admin
	 */
	@RequestMapping (value="/admin/gettutoringcenterform.do", method=RequestMethod.GET)
	public String getTutoringCenterForm(Map model){
		model.put("tutoringCenter",new TutoringCenter());
		return "tutoring_center_form";
	}
	@RequestMapping (value="/admin/addtutoringcenter.do", method=RequestMethod.POST)
	public String addTutorialCenter (@Valid @ModelAttribute("tutorialCenter") final TutoringCenter tutorialCenter,
			BindingResult result) throws PortalException{
		if(!(tutorialCenter==null)){
			tutoringCenterService.createOrSave(tutorialCenter);
			return "redirect:/admin/gettutoringcenter.do";
		}
		return "redirect:/admin/gettutoringcenter.do";
	}
	@RequestMapping (value="/admin/gettutoringcentereditpage.do", method=RequestMethod.GET)
	public String getEditPage(@RequestParam("id") final String id, Map model) throws PortalException{
		if(id!=null){
			TutoringCenter tutoringCenter=tutoringCenterService.getTutoringCenterById(id);
			model.put("tutoringCenter", tutoringCenter);
			return "tutoring_center_edit_page";
		}
		return "redirect:/admin/gettutoringcenter.do";

}
	@RequestMapping(value="/admin/addeditedtutorialcenter.do", method=RequestMethod.POST)
	 public String editTutoringCenter(@Valid @ModelAttribute("tutoringCenter")final TutoringCenter tutoringCenter, BindingResult bindingResult,Map Model){
		 if(tutoringCenter!=null){
			 TutoringCenter tutorialCenter= tutoringCenterService.getTutoringCenterById(tutoringCenter.getId());
			tutorialCenter.setHeader(tutoringCenter.getHeader());
			tutorialCenter.setTerm(tutoringCenter.getTerm());
			tutorialCenter.setTutoringSchedule(tutoringCenter.getTutoringSchedule());
			tutorialCenter.setCourses(tutoringCenter.getCourses());
			tutorialCenter.setContact(tutoringCenter.getContact());
			/*
			 * save it
			 */
			tutoringCenterService.createOrSave(tutorialCenter);
			return "redirect:/admin/gettutoringcenter.do";
		 }
		return "redirect:/admin/gettutoringcenter.do";
	 }
	}
